package classes;

import java.awt.image.BufferedImage;

/**
 * EdgeDetection Class
 * 
 * @author Nicole Nelson
 */
public class EdgeDetection 
{
	private int BLACK = -16777216;
	private int WHITE = -1;
	private int current = 0;
	private double average = 0.0;
	private int[] rowCount;
	private int[] colCount;
	private BufferedImage original;
	
	/*
	 * Sets the Original Image
	 */
	public void setOriginal(BufferedImage image)
	{
		original = image;
		findEdges(image);
	}
	
	/*
	 * Creates an edge image and calculates the pixel variance average
	 */
	public double findEdges(BufferedImage image)
	{
		int imgWidth = image.getWidth();
		int imgHeight = image.getHeight();
		
		BufferedImage edge = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
		rowCount = new int[imgHeight];
		colCount = new int[imgWidth];
		for (int row = 0; row < rowCount.length; row++)
			rowCount[row] = 0;
		for (int col = 0; col < colCount.length; col++)
			colCount[col] = 0;
		
		
		for (int height = 0; height < imgHeight; height++)
		{
			current = image.getRGB(0, height);
			for (int width = 0; width < imgWidth; width++)				
			{
				if(image.getRGB(width, height) != current)
				{
					current = image.getRGB(width, height);
					edge.setRGB(width, height, BLACK);
				}
				else
					edge.setRGB(width, height, WHITE);
			}
		}
		
		for (int width = 0; width < imgWidth; width++)
		{
			current = image.getRGB(width, 0);
			for (int height = 0; height < imgHeight; height++)
			{
				if(image.getRGB(width, height) != current)
				{
					current = image.getRGB(width, height);
					edge.setRGB(width, height, BLACK);
				}
			}
		}
		
		for (int height = 0; height < imgHeight; height++)
			for (int j = 0; j < imgWidth; j++)
				if(image.getRGB(j, height) == BLACK)
					rowCount[height]++;
		
		for (int width = 0; width < imgWidth; width++)
			for (int height = 0; height < imgHeight; height++)
				if(image.getRGB(width, height) == BLACK)
					colCount[width]++;
		
		average = 0.0;
		int total = 0;
		for (int row = 0; row < rowCount.length; row++)
		{
			
			average += rowCount[row];
			if(rowCount[row] != 0)
				total++;
		}
		
		for (int col = 0; col < colCount.length; col++)
		{
			average += colCount[col];
			if(colCount[col] != 0)
				total++;
		}
		
		average /= total;
		
		return average;
	}
	
	/*
	 * Crops the stored image to where the QR Code is
	 */
	public BufferedImage crop()
	{
		int top = 0;
		int left = 0;
		int bottom = rowCount.length - 1;
		int right = colCount.length - 1;

		for (int row = 0; row <= bottom; row++)
			for (int col = 0; col <= right; col++)
				if(rowCount[row] + colCount[col] > average * 2 && rowCount[row] > 0 && colCount[col] > 0)
				{
					top = row;
					left = col;
					row = rowCount.length;
					col = colCount.length;
				}
		
		bottom = top + 1;
		right = left + 1;
		while(rowCount[bottom] > 0 || colCount[right] > 0)
		{
			if (bottom == rowCount.length - 1 || right == colCount.length - 1)
			{
				break;
			}
			bottom++;
			right++;
		}
		
		int imgWidth = right - left;
		int imgHeight = bottom - top;

		BufferedImage crop = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
		for (int height = 0; height < imgHeight; height++)
			for (int width = 0; width < imgWidth; width++)
				crop.setRGB(width, height, original.getRGB(width + left, height + top));
		
		return crop;
		
	}
}
